package com.gxuwz.medical.tools;

import org.joda.time.DateTime;
import org.joda.time.Period;

/**
 * 日期组件
 * @author 
 *
 */
public class DateUtil {
	
	public static final int DAY = 1;
	public static final int WEEK = 2;
	public static final int MONTH = 3;
	public static final int YEAR = 4;
	
	public static java.util.Date parse(String datetime){
		DateTime dateTime =new DateTime();
		return dateTime.toDate();
	}
	/**
	 * 获得日期差值
	 * @param starttime
	 * @param endtime
	 * @param dateType-{1:day,2:week,3:month,4:year}
	 * @return
	 */
	public static int diff(String starttime,String endtime,int dateType ){
		 DateTime start=new DateTime(starttime);
		  DateTime end= null;
		 if(endtime==null){
			 end=new DateTime();
		 }else{
			 end=new DateTime(endtime);
		 } 
	     Period period=new Period(start,end);
	     int diffVal=-1;
	    switch (dateType) {
		case DAY:
			diffVal= period.getDays();
			break;
		case WEEK:
			diffVal= period.getWeeks();
			break;
		case MONTH:
			diffVal= period.getMonths();
			break;
		case YEAR:
			diffVal= period.getYears();
			break;
		default:
			break;
		}
	    return diffVal;
	    
	}
	/**
	 * 计算距离当前系统时间的年份数
	 * @param starttime
	 * @return
	 */
	public static int diffYear(String starttime){ 
		return diff(starttime, null, YEAR);
	}
	public static void main(String[]args){
		String starttime="1949-10-1";
		String endtime ="2019-10-1";
		int year=DateUtil.diff(starttime, endtime, 4); 
		System.out.println("year:"+year);
	}

}
